package 编程能力入门1;

import java.util.ArrayList;
import java.util.List;

public class _10仅执行一次字符串交换能否使两个字符串相等 {
    public boolean areAlmostEqual(String s1, String s2) {
        if(s1 == null || s2 == null) return false;
        if(s1.equals(s2)) return true;
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < s1.length(); i++) {
            char c1 = s1.charAt(i);
            char c2 = s2.charAt(i);
            if(c1!=c2){
                res.add(i);
            }
        }
        if(res.size() == 0) return true;
        if(res.size() == 2){
            int p1 = res.get(0);
            int p2 = res.get(1);
            return s1.charAt(p1) == s2.charAt(p2) && s1.charAt(p2) == s2.charAt(p1);
        }
        return false;
    }
}
